﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Lib.Common;

namespace Lib.CommonForm
{
    public class Splash:Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Splash));
            this.lblProjectName = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel2 = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // lblProjectName
            // 
            this.lblProjectName.AutoSize = true;
            this.lblProjectName.BackColor = System.Drawing.Color.Transparent;
            this.lblProjectName.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblProjectName.ForeColor = System.Drawing.SystemColors.ControlText;
            this.lblProjectName.Location = new System.Drawing.Point(48, 12);
            this.lblProjectName.Name = "lblProjectName";
            this.lblProjectName.Size = new System.Drawing.Size(182, 13);
            this.lblProjectName.TabIndex = 2;
            this.lblProjectName.Tag = "";
            this.lblProjectName.Text = "Xin vui lòng chờ trong giây lát...";
            this.lblProjectName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.Transparent;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.lblProjectName);
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(240, 40);
            this.panel1.TabIndex = 3;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.pictureBox1);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(48, 38);
            this.panel2.TabIndex = 3;
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(5, 3);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(31, 31);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // 
            // Splash
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.ClientSize = new System.Drawing.Size(240, 40);
            this.ControlBox = false;
            this.Controls.Add(this.panel1);
            this.DoubleBuffered = true;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "Splash";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "frmSplash";
            this.Load += new System.EventHandler(this.frmSplash_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label lblProjectName;
        private Panel panel1;
        private Panel panel2;
        private PictureBox pictureBox1;
        private MethodInvoker method;

        string error = "";

        public string Error
        {
            get { return error; }
            set { error = value; }
        }

        public Splash(MethodInvoker method, string message)
        {
            InitializeComponent();

            this.BackColor = System.Drawing.SystemColors.Window;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            Control.CheckForIllegalCrossThreadCalls = false;

            //this.pictureBox1.Image = global::PLSP.Properties.Resources._10;

            this.method = method;
            BackgroundWorker bgJ = new BackgroundWorker();
            bgJ.DoWork += new DoWorkEventHandler(bgJ_DoWork);
            bgJ.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgJ_RunWorkerCompleted);
            bgJ.RunWorkerAsync();

            if (message.Length > 0)
                lblProjectName.Text = message;
            else
                lblProjectName.Text = "Hãy đợi giây lát...";//please wait a moment
            int MinWidth = 240;
            int ReWidth = lblProjectName.Width + lblProjectName.Location.X + 10;
            if (ReWidth > MinWidth)
                this.Width = ReWidth;
            else
                this.Width = MinWidth;
        }
        void bgJ_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Globals.Application_SetCultureInfo(System.Threading.Thread.CurrentThread);
                if (method != null)
                    method.Invoke();

            }
            catch (Exception ex)
            {
                error = ex.Message;
                e.Result = false;
            }

        }
        void bgJ_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Close();
        }

        private void frmSplash_Load(object sender, EventArgs e)
        {
            this.Refresh();
        }

        public static void Show(MethodInvoker DoWork, string message)
        {
            Splash f = new Splash(DoWork, message);
            f.ShowDialog();
            String msg = f.Error;

            if (msg.Length > 0)
            {
                f.Close();
                FormGlobals.Message_Error(new Exception( msg));
            }

        }
        public static void Show(MethodInvoker DoWork)
        {
            Show(DoWork, "");
        }
    }
}